package com.huitone.gddw.controller.monitoring.base;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.WarnRecv;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.service.dto.monitoring.base.WarnRecvDTO;
import com.huitone.gddw.service.monitoring.base.IWarnRecvService;
import com.huitone.gddw.utils.SysLoginUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 预警接收人
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="warnRecv")
public class WarnRecvController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(WarnRecvController.class);
	
	@Autowired
	private IWarnRecvService warnRecvService;
	
	/**
	 * 根据对象ID获取预警接收人信息
	 * @param objId 对象ID
	 * @return List<WarnRecvDTO>
	 */
	@ResponseBody
	@RequestMapping(value="getByObjId")
	public List<WarnRecvDTO> getByObjId(String objId, Integer warnLevel) {
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("objId", objId);
			params.put("warnLevel", warnLevel);
			return warnRecvService.select(params);
		} catch (Exception e) {
			LOGGER.error("根据对象ID获取预警接收人信息时出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param objId 对象ID
	 * @param request
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(String objId, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]保存预警接收人信息";
		try {
			List<WarnRecv> listSave = new ArrayList<WarnRecv>();
			List<WarnRecv> listUpdate = new ArrayList<WarnRecv>();
			List<WarnRecv> listDel = new ArrayList<WarnRecv>();
			List<String> listRecId = new ArrayList<String>();
			
			String warnRecvs = URLDecoder.decode(request.getParameter("warnRecvs"), "UTF-8");
			JSONArray ja = JSONArray.fromObject(warnRecvs);
			// 遍历
			for (int i = 0; i < ja.size(); i++) {
				WarnRecv warnRecv;
				JSONObject jb = ja.getJSONObject(i);
				String recId = jb.getString("recId");
				String userId = jb.getString("userId");
				String userType = jb.getString("userType");
				int warnLevel1 = jb.getInt("warnLevel1");
				int warnLevel2 = jb.getInt("warnLevel2");
				int warnLevel3 = jb.getInt("warnLevel3");
				if (StringUtils.isBlank(recId)) {
					warnRecv = new WarnRecv();
					warnRecv.setObjId(objId);
					warnRecv.setUserId(userId);
					warnRecv.setUserType(userType);
					warnRecv.setWarnLevel1(warnLevel1);
					warnRecv.setWarnLevel2(warnLevel2);
					warnRecv.setWarnLevel3(warnLevel3);
					listSave.add(warnRecv);
				} else {
					warnRecv = warnRecvService.getById(recId);
					if (userId.equals(warnRecv.getUserId()) && userType.equals(warnRecv.getUserType()) 
							&& warnLevel1 == warnRecv.getWarnLevel1() && warnLevel2 == warnRecv.getWarnLevel2() 
							&& warnLevel3 == warnRecv.getWarnLevel3()) {
						listRecId.add(recId);
						continue;
					}
					warnRecv.setUserId(userId);
					warnRecv.setUserType(userType);
					warnRecv.setWarnLevel1(warnLevel1);
					warnRecv.setWarnLevel2(warnLevel2);
					warnRecv.setWarnLevel3(warnLevel3);
					listUpdate.add(warnRecv);
					listRecId.add(recId);
				}
			}
			
			List<WarnRecv> listWarnRecv = warnRecvService.getByObjId(objId);
			for (WarnRecv warnRecv : listWarnRecv) {
				if (listRecId.contains(warnRecv.getRecId())) {
					continue;
				}
				listDel.add(warnRecv);
			}
			warnRecvService.save(listSave, listUpdate, listDel);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存预警接收人信息信息时出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
}
